package com.stox.ui.javafx.core.launcher;

import javafx.application.Platform;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import com.stox.ui.javafx.core.widget.StoxStage;
import com.stox.util.ApplicationContextUtil;

public class ApplicationInitializerRunnable implements Runnable{

	private Stage primaryStage;
	
	public void initialize(Stage stage) {
		ExceptionHandler exceptionHandler = new ExceptionHandler();
		Thread.currentThread().setUncaughtExceptionHandler(exceptionHandler);
		
		this.primaryStage = stage;
		
		stage.setMaximized(true);
		stage.initStyle(StageStyle.TRANSPARENT);
		Group group = new Group();
		Scene scene  = new Scene(group, Color.TRANSPARENT);
		stage.setScene(scene);
		addSplashNode(group);
		stage.show();	
		new Thread(this).start();
	}
	
	@Override
	public void run() {
		ApplicationContextUtil.loadApplicationContext();
		
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				primaryStage.close();
				StoxStage stage = ApplicationContextUtil.getApplicationContext().getBean(StoxStage.class);
				stage.show();
			}
		});
		
	}
	
	
	public void addSplashNode(Group group) {
		Text text = new Text(Screen.getPrimary().getVisualBounds().getWidth()/2 - 200,
				Screen.getPrimary().getVisualBounds().getHeight()/2, "STOX");
		text.setFill(Color.BLUEVIOLET);
		text.setFont(Font.font("Goudy Old Style", FontWeight.BOLD, 150));
		text.setStrokeLineCap(StrokeLineCap.ROUND);
		text.setTextAlignment(TextAlignment.CENTER);
		text.setTextOrigin(VPos.CENTER);
		
		DropShadow ds = new DropShadow();
		ds.setOffsetY(20f);
		ds.setOffsetX(30f);
		ds.setHeight(50);
		ds.setWidth(50);
		ds.setRadius(10);
		ds.setColor(Color.DARKBLUE);
		text.setEffect(ds);
		
		group.getChildren().add(text);
	}
	
}
